package com.uva.pgpi.sgcc.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;


public final class DateUtils
{
	
	public static final String DATE_NORMAL = "dd/MM/yyyy";
	public static final String DATE_NORMAL_PLUS_TIME = "dd/MM/yyyy HH:mm";
    public static final String DATE_EXTENDED = "EEE dd MMM HH:mm:ss z yyyy";
    
    private DateUtils()
    {
    }
    
    public static DateUtils getInstance()
    {
      if (ref == null)
          // it's ok, we can call this constructor
          ref = new DateUtils();
      return ref;
    }

    private static DateUtils ref;
    
    public static final String millisToDate(long millis){
		 
        return DateUtils.millisToDate(millis, DateUtils.DATE_NORMAL_PLUS_TIME);
    }    
    
    public static final String millisToDate(long millis, String format){
		 
		Date now = new Date(millis);
        return  new SimpleDateFormat(format).format(now);
    }
    
    public static final long StringToLong(String fecha) {
		return DateUtils.StringToLong(fecha, DateUtils.DATE_NORMAL_PLUS_TIME);

	}
    
    public static final long StringToLong(String fecha, String format){
    	
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        try
        {
            Date date = new Date();
            date = simpleDateFormat.parse(fecha);
           // System.out.println("date : "+simpleDateFormat.format(date));
            return date.getTime();
        }
        catch (Exception ex)
        {
            System.out.println("Exception "+ex);
        }
    	
    	return 0;
    }
    
    /***
     * http://stackoverflow.com/questions/1555262/calculating-the-difference-between-two-java-date-instances
     * @param value
     * @return
     */
    private static final String fill2(int value)
    {
        String erg = String.valueOf(value);

        if (erg.length() < 2)
            erg = "0" + erg;            
        return erg;
    }
    
    public static final String getDuration(long diffInMillies)
    {                   
        TimeUnit timeUnit = TimeUnit.SECONDS;
        
        long s = timeUnit.convert(diffInMillies, TimeUnit.MILLISECONDS);

        long days = s / (24 * 60 * 60);
        long rest = s - (days * 24 * 60 * 60);
        long std =  rest / (60 * 60);
        long rest1 = rest - (std * 60 * 60);
        long min = rest1 / 60;      
        long sec = s % 60;

        String dates = "";
        if (days > 0)
            dates += days + " Days ";

        dates += fill2((int) std) + "h ";
        dates += fill2((int) min) + "m ";
        dates += fill2((int) sec) + "s ";

        return dates;
    }
    
    
    public static void main(String[] args) {
		long m = DateUtils.StringToLong("27/07/2013 12:54");
		System.out.println(DateUtils.millisToDate(m));
		
		System.out.println(DateUtils.getDuration(1000));
	}
    
}


